$.ajaxSetup({
    type: "POST",
    dataType: "json",
    global: true
});
var load=false;
function loading(t){
    if(t==undefined||t=='start'){
        load=true;
        t=true;
    }
    else if(t=='stop'){
        load=false;
        t=false;
    }
    o=parseFloat($("#symbiosis").css('opacity'));
    if(o==0.2) t=true;
    else if(o==1) t=false;
    if(!t){
        $("#symbiosis").css('opacity', o-0.1);
    }
    else{
        $("#symbiosis").css('opacity', o+0.1);
    }
    if(load==true||o!=1){
        setTimeout(function(){
            loading(t);
        }, 100);
    }
}
$('#symbiosis').ajaxStart(loading('start'));
$('#symbiosis').ajaxStop(loading('stop'));

$(document).ready(function(){
    $('.s3_url').val($('#url').text());
    $.ajax({
        url: "ajax/step1.php",
        data: {
            "type":"config"
        },
        success: function(r){
            if(r!=null&&r.version!=undefined&&r.version) $('.s1_config_version').html(r.version).before('');
            else{
                val=$('.s1_config').html();
                $('.s1_config').after("<span class='s1_config'>"+val+"</span>").css('display','none');
                $('.s1_config').css('opacity', '0.5');
            }
            
        },
        error: function(r){
            val=$('.s1_web').html();
            $('.s1_web').after("<span class='s1_web'>"+val+"</span>").css('display','none');
            $('.s1_web').css('opacity', '0.5');
        }
    });
    $.ajax({
        url: "ajax/step1.php",
        data: {
            "type":"archive"
        },
        success: function(r){
            if(r.version!=undefined&&r.version) $('.s1_archive_version').html(r.version).before('version ');
            else{
                val=$('.s1_archive').html();
                $('.s1_archive').after("<span class='s1_archive'>"+val+"</span>").css('display','none');
                $('.s1_archive').css('opacity', '0.5');
            }
        },
        error: function(r){
            val=$('.s1_archive').html();
            $('.s1_archive').after("<span class='s1_archive'>"+val+"</span>").css('display','none');
            $('.s1_archive').css('opacity', '0.5');
        }
    });
    $.ajax({
        url: "ajax/step1.php",
        data: {
            "type":"web"
        },
        success: function(r){
            if(r!=null&&r.version!=undefined&&r.version) $('.s1_web_version').html(r.version).before('version ');
            else{
                val=$('.s1_web').html();
                $('.s1_web').after("<span class='s1_web'>"+val+"</span>").css('display','none');
                $('.s1_web').css('opacity', '0.5');
            }
            
        },
        error: function(r){
            val=$('.s1_web').html();
            $('.s1_web').after("<span class='s1_web'>"+val+"</span>").css('display','none');
            $('.s1_web').css('opacity', '0.5');
        }
    });
    var step=0;
    $('.slider').lslider({
        'type': 'alpha'
    });
    $('.slider .steps img').css({
        'opacity': '0.2'
    });
    $('.slider .steps img:first').css({
        'opacity': '1'
    }).addClass('active');
    $('.slider .steps a').click(function(){
        if($(this).find('img').hasClass('active')) return false;
        s=$(this).attr('href').substr(1);
        if(s>step) return false;
        $('.slider').lslider('goto', s);
        $('.slider .steps img.active').animate({
            'opacity': '0.5'
        }).removeClass('active');
        $(this).find('img').animate({
            'opacity': '1'
        }).addClass('active');
        return false;
    });
    
    $('.s1_config').click(function(){
        $('.s1').html('<span>Ok=)</span>');
        setTimeout(function(){
            $('.step2').click();
        },1000);
        step=1;
        $('.step2').find('img').animate({
            'opacity': '0.5'
        });
    });
    
    $('.s1_archive').click(function(){
        $.ajax({
            url: "ajax/step1.php",
            data: {
                "type":"install_archive",
                "version": $('.s1_archive_version').html()
            },
            success: function(r){
                if(r.success){
                    $('.s1').html('<span>Extracting complate</span>');
                    setTimeout(function(){
                        $('.step2').click();
                    },1000);
                    step=1;
                    $('.step2').find('img').animate({
                        'opacity': '0.5'
                    });
                    
                }
            },
            error: function(r){
                
            }
        });
        return false;
    });
    
    $('.s1_web').click(function(){
        $.ajax({
            url: "ajax/step1.php",
            data: {
                "type":"install_web",
                "version": $('.s1_web_version').html()
            },
            success: function(r){
                if(r.success){
                    $('.s1').html('<span>Extracting complate</span>');
                    step=1;
                    $('.step2').find('img').animate({
                        'opacity': '0.5'
                    });
                    setTimeout(function(){
                        $('.step2').click();
                    },1000);
                }
            },
            error: function(r){
                
            }
        });
        return false;
    });
    
    $('.s2_host, .s2_user, .s2_pass, .s2_db').blur(function(){
        if($('.s2_host').val()!=''&&$('.s2_user').val()!=''&&$('.s2_db').val()!=''){
            $.ajax({
                url: "ajax/step2.php",
                data: {
                    "type":"config",
                    "host":$('.s2_host').val(),
                    "user":$('.s2_user').val(),
                    "pass":$('.s2_pass').val(),
                    "db":$('.s2_db').val()
                },
                success: function(r){
                    res='';
                    if(r.connect) res+='Sucsesfully connection to the database control system.<br/>';
                    else res+='Connection error to the database.<br/>';
                    
                    if(r.choose) res+='Database was choose sucsesfully.<br/>';
                    else res+='Database error choosen.<br/>';
                    
                    $('.s2_res').html(res);
                    if(r.connect&&r.choose){
                        step=2;
                        $('.step3').find('img').animate({
                            'opacity': '0.5'
                        });
                        setTimeout(function(){
                            $('.step3').click();
                        },1000);
                    }
                    else{
                        step=1;
                        $('.step3').find('img').animate({
                            'opacity': '0.2'
                        });
                    }
                },
                error: function(r){
                    
                }
            });
        }
    });
    
    $('.s2_save').click(function(){
        $('.s2_db').blur();
        return false;
    });
    
    $('.s3_url').blur(function(){
        if($('.s3_url').val()!=''){
            $.ajax({
                url: "ajax/step3.php",
                data: {
                    "type":"config",
                    "url":$('.s3_url').val()
                },
                success: function(r){
                    if(r.saved) res='Saved.<br/>';
                    else res='Error.<br/>';
                    
                    $('.s3_res').html(res);
                    if(r.saved){
                        step=3;
                        $('.step4').find('img').animate({
                            'opacity': '0.5'
                        });
                        setTimeout(function(){
                            $('.step4').click();
                        },1000);
                    }
                    else{
                        step=2;
                        $('.step4').find('img').animate({
                            'opacity': '0.2'
                        });
                    }
                },
                error: function(r){
                    
                }
            });
        }
    });
    
    $('.s3_save').click(function(){
        $('.s3_url').blur();
        return false;
    });
    
    
    var symbols=['{', '}', '[', ']', ':', ';', '|', '.', ',', '<', '>', '?', '=', '+', '-', '(', ')', '_', '*', '&', '^', '%', '$', '#', '@', '!', '~'];
    
    $('.password input').keyup(function(){
        count_char=0;
        count_number=0;
        count_symbol=0;
        stars=0;
        error=false;
        
        div=$(this).parent().parent().parent().parent();
        val=$(this).val();
        for(key in val){
            ch=val[key];
            code=ch.charCodeAt(0);
            if(code>=97&&code<=122||code>65&&code<=90){
                count_char++;
            }
            else if(code>=48&&code<=57){
                count_number++;
            }
            else{
                error=true;
                for(k in symbols){
                    v=symbols[k];
                    if(ch==v){
                        count_symbol++;
                        error=false;
                        break;
                    }
                }
            }
        }
        if(error){
            div.css('background','url(files/img/password-error.png) 100% 1px no-repeat');
        }
        else if(val.length<4){
            div.css('background','url(files/img/password-0.png) 100% 1px no-repeat');
        }
        else{
            stars+=Math.floor(val.length/6);
            if(count_number>2&&count_char>2) stars++;
            if(count_symbol>0) stars++;
            if(stars>3) stars=3;
            div.css('background','url(files/img/password-'+stars+'.png) 100% 1px no-repeat');
        }
    });
});